package com.qingyun.video.controller;



import com.qingyun.model.common.dtos.R;
import com.qingyun.model.user.pojos.User;
import com.qingyun.model.video.pojos.Like;
import com.qingyun.model.video.pojos.Video;
import com.qingyun.video.service.VideoService;
import com.qingyun.video.service.LikeService;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/like")
public class LikeController {


    @Resource
    private LikeService likeService;

    @Resource
    private VideoService videoService;



    @PostMapping("/like/{videoId}")
    public R like(@PathVariable Long videoId){
        User user=AppThreadLocalUtil.getUser();
        Video video = videoService.query().eq("id", videoId).one();
        if (likeService.isLike(videoId)){


//       User user=new User();
//       user.setId(1302865306489733122L);

            Like like=new Like();
            like.setVideoId(videoId);
            like.setUserId(user.getId());
            likeService.save(like);
            video.setLikes(video.getLikes()+1);
            videoService.updateById(video);
            return R.success("点赞成功");
        }


//        User user=new User();
//        user.setId(1302865306489733123L);//测试时把次注解与上方User线程获取互换，设定一个一个死的user_id


        Like like=likeService.query().eq("video_id",videoId).eq("user_id",user.getId()).one();
        likeService.removeById(like.getId());
        video.setLikes(video.getLikes()-1);
        videoService.updateById(video);
        return R.success("取消点赞");



    }




    /**
     * 查询收藏√
     * @return
     */

    @GetMapping("/content")
    public R queryLike(){
        List<Like> list = likeService.query().eq("user_id", AppThreadLocalUtil.getUser().getId()).list();
        List<Video> videoList=new ArrayList<>();
        for (Like like : list){
            Video video= videoService.query().eq("id",like.getVideoId()).one();
            videoList.add(video);

        }

        return R.success(videoList);
    }

}
